//
//  CreateQuanziView.m
//  YiqiMobile
//
//  Created by henry on 13-4-30.
//  Copyright (c) 2013年 henry. All rights reserved.
//


#define TONGZHILABELTAG 240
#define TONGZHIDETAILLABELTAG 241
#import "CreateTongzhiView.h"
#import "QuartzCore/QuartzCore.h"
#import "Config.h"
#import "Utility.h"
#include "DefineAction.h"
#import "AppDelegate.h"
#import "JSON.h"
#import "AppDelegate.h"
#import "MBProgressHUD.h"



@implementation CreateTongzhiView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
         self.backgroundColor=[UIColor colorWithRed:159.0/255 green:159.0/255 blue:159.0/255 alpha:0.5];
        UIView *createview=[[UIView alloc] initWithFrame:CGRectMake(20, 80, 280, 168)];
        createview.layer.cornerRadius=10;
        createview.layer.masksToBounds=YES;
        createview.backgroundColor=[UIColor whiteColor];
        [self addSubview:createview];
        
        UILabel *tongzhiLabel=[Utility detailTextLabel:CGRectMake(12, 16, 50, 20) andFontSize:15];
        tongzhiLabel.text=@"发通知";
        tongzhiLabel.tag=TONGZHILABELTAG;
        [createview addSubview:tongzhiLabel];
        UILabel *tongzhiDetailLabel=[Utility detailTextLabel:CGRectMake(62, 23, 180, 13) andFontSize:9];
        tongzhiDetailLabel.tag=TONGZHIDETAILLABELTAG;
        tongzhiDetailLabel.text=@"/通知圈子成员可见";
        [createview addSubview:tongzhiDetailLabel];
        
        UIImageView *inputBg=[[UIImageView alloc] initWithFrame:CGRectMake(10, 39, 260, 38)];
        inputBg.image=[Utility imageNamed:@"publishnoti_bg@2x.png"];
        [createview addSubview:inputBg];
        self.subject = [self customTextField:CGRectMake(18, 40, 244, 36)];
        self.subject.delegate=self;
        [createview
         addSubview:self.subject];
        
        UIImageView *inputBg2=[[UIImageView alloc] initWithFrame:CGRectMake(10, 84, 260, 38)];
        inputBg2.image=[Utility imageNamed:@"publishnoti_bg@2x.png"];
        [createview addSubview:inputBg2];
        self.content = [self customTextField:CGRectMake(18, 85, 244, 36)];
        self.content.delegate=self;
        [createview
         addSubview:self.content];
        
        
        
        [createview addSubview:[Utility customButton:self action:@selector(saveAction:) normalImage:@"publishcannel_normal.png" lightedImage:@"publishcannel_normal.png" frame:CGRectMake(146, 128, 63, 25) title:nil tag:66]];
        [createview addSubview:[Utility customButton:self action:@selector(saveAction:) normalImage:@"publishok_normal.png" lightedImage:@"publishok_normal.png" frame:CGRectMake(210, 128, 63, 25) title:nil tag:67]];

        // Initialization code
    }
    return self;
}

-(void) showData{
    UILabel *tongzhilabel=(UILabel*)[self viewWithTag:TONGZHILABELTAG];
     UILabel *tongzhiDetaillabel=(UILabel*)[self viewWithTag:TONGZHIDETAILLABELTAG];
    if (self.type==0) {
        tongzhilabel.text=@"发通知";
        tongzhiDetaillabel.text=@"/通知圈子成员可见";
    }else{
        tongzhilabel.text=@"发公告";
        tongzhiDetaillabel.text=@"/公告所有用户可见,成员和关注者又提示";
    }

}

#pragma -mark UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    if (textField==self.subject) {
        if (self.subject.text.length>0) {
            return YES;
        }
    }else if(textField==self.content){
        if (self.content.text.length>0) {
            [self.content resignFirstResponder];
            return YES;
        }
    }
    return NO;
}

-(void) saveAction:(UIButton*) sender{
    if (sender.tag==66) {//取消
        [self removeFromSuperview];
        return;
    }
    //保存数据
    NSString *subject=self.subject.text;
    NSString *content=self.content.text;
    NSString *userId=[[NSUserDefaults standardUserDefaults] objectForKey:USERID];
    AppDelegate *app=(AppDelegate*)[[UIApplication sharedApplication] delegate];
    if (app.status==NotReachable) {
        [Utility customAlertWithMsg:NETWORTERROE andTitle:ALRRTTITLE andDelagate:nil];
        return;
    }
    if (content==nil||[content isEqualToString:@""]) {
        [Utility customAlertWithMsg:@"请输入内容" andTitle:ALRRTTITLE andDelagate:nil];
        return;
	}
    if (subject==nil||[subject isEqualToString:@""]) {
        [Utility customAlertWithMsg:@"请输入标题" andTitle:ALRRTTITLE andDelagate:nil];
        return;
	}
    NSString *url=[NSString stringWithFormat:@"%@/%@",[Utility  getServerUrl],SENDTONGZHIACTION];
        NSMutableDictionary *param=[NSMutableDictionary dictionary];
    if (self.type==0) {
        /*etz.u0	N	发起人ID
        etz.etz1	N	标题
        etz.etz3	N	内容
        etz.t0	N	内容类型
        etz.e0	N	所属圈子ID*/
        [param setObject:userId forKey:@"etz.u0"];
        [param setObject:subject forKey:@"etz.etz1"];
        [param setObject:content forKey:@"etz.etz3"];
        [param setObject:[NSString stringWithFormat:@"%d",self.type] forKey:@"etz.t0"];
        [param setObject:app.quanziId forKey:@"etz.e0"];
    }else{
        url=[NSString stringWithFormat:@"%@/%@",[Utility  getServerUrl],SENDGGAOACTION];
        [param setObject:userId forKey:@"gg.u0"];
        [param setObject:subject forKey:@"gg.gg1"];
        [param setObject:content forKey:@"gg.gg2"];
        [param setObject:[NSString stringWithFormat:@"%d",self.type] forKey:@"gg.t0"];
        [param setObject:app.quanziId forKey:@"gg.e0"];
    }
    
        self.request=[[URLRequest alloc] initWithPostURL:url andDelegate:self andPostData:param andFile:nil];
        [self.request startLoadData];
}
- (void)requestFinished:(id)context{
    @try{
        NSDictionary *result=[context JSONValue];
        if (result!=nil&&[[result objectForKey:@"code"] integerValue]==0) {
            if ([[result objectForKey:@"method"] isEqualToString:@"sendTomgzhi"]) {
             [Utility customAlertWithMsg:@"发布通知成功" andTitle:ALRRTTITLE andDelagate:nil];
                [self removeFromSuperview];
              if ([_delegate respondsToSelector:@selector(success:)]) {
                  [_delegate  success:0];
                }
    
            }else if ([[result objectForKey:@"method"] isEqualToString:SENDGGAOACTION]) {
                 [Utility customAlertWithMsg:@"发布公告成功" andTitle:ALRRTTITLE andDelagate:nil];
                [self removeFromSuperview];
                [_delegate  success:1];
            }
            
            
            //[Utility customAlertWithMsg:@"保存成功" andTitle:ALRRTTITLE andDelagate:nil];
           
        }else{
            [Utility customAlertWithMsg:PROCESSERROE andTitle:ALRRTTITLE andDelagate:nil];
        }
    }@catch (NSException *e) {
        [Utility customAlertWithMsg:NETWORTERROE andTitle:ALRRTTITLE andDelagate:nil];
    }
}

- (void)requestFailed:(id)context error:(NSString *)error{
    [Utility customAlertWithMsg:NETWORTERROE andTitle:ALRRTTITLE andDelagate:nil];
}

-(UITextField*) customTextField:(CGRect) frame{
    
    UITextField *textFied = [[UITextField alloc] initWithFrame:frame];
    textFied.keyboardType=UIKeyboardAppearanceDefault;
    textFied.autocorrectionType = UITextAutocorrectionTypeNo;
    textFied.returnKeyType=UIReturnKeyNext;
    textFied.clearButtonMode=UITextFieldViewModeWhileEditing;
    textFied.contentVerticalAlignment=UIControlContentVerticalAlignmentCenter;
    textFied.backgroundColor=[UIColor  clearColor];
    textFied.borderStyle=UITextBorderStyleNone;
    textFied.textColor=[UIColor blackColor];
    textFied.font=[UIFont fontWithName:FONT_FAMILY size:15.0f];
    textFied.autocorrectionType=UITextAutocorrectionTypeNo;
    [textFied setBorderStyle:UITextBorderStyleNone];
    textFied.returnKeyType = UIReturnKeyDone;
    textFied.keyboardType=UIKeyboardTypeDefault;
    return textFied;
}


@end
